/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author AAN
 */
public class ModelPromo {

    Connection conn;

    public ModelPromo(Connection connection) {
       
    }

    public List<Promo> getAllPromo() throws Exception {
        List<Promo> promo = new ArrayList<Promo>();
        String sql = "SELECT * FROM promo";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Promo prm = new Promo();
                prm.setId_promo(rs.getInt(1));
                prm.setNama_promo(rs.getString(2));
                prm.konversiTanggalmulai(rs.getDate(3));
                prm.konversiTanggalhabis(rs.getDate(4));
                prm.setLokasi(rs.getString(7));
                prm.setKategori(rs.getString(6));
                prm.setDeskripsi(rs.getString(5));
                promo.add(prm);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return promo;
    }

    //one
    public List<Promo> selectone(int id) throws Exception {
        List<Promo> promo = new ArrayList<Promo>();
        String sql = "SELECT * FROM promo where id_promo = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, id);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Promo prm = new Promo();
                prm.setId_promo(rs.getInt(1));
                prm.setNama_promo(rs.getString(2));
                prm.konversiTanggalmulai(rs.getDate(3));
                prm.konversiTanggalhabis(rs.getDate(4));
                prm.setLokasi(rs.getString(7));
                prm.setKategori(rs.getString(6));
                prm.setDeskripsi(rs.getString(5));
                promo.add(prm);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return promo;
    }

    public void add(String nama_promo, Date mulai_promo, Date habis_promo, String deskripsi, String kategori, String lokasi) throws Exception {
        String sql = "INSERT INTO promo VALUES (NULL, ?, ?, ?, ?, ?, ?)";
        java.sql.Date mulaipromo = new java.sql.Date(mulai_promo.getTime());
        java.sql.Date habispromo = new java.sql.Date(habis_promo.getTime());
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, nama_promo);
            st.setDate(2, mulaipromo);
            st.setDate(3, habispromo);
            st.setString(4, deskripsi);
            st.setString(5, kategori);
            st.setString(6, lokasi);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void update(int id, String nama_promo, Date mulai_promo, Date habis_promo, String deskripsi, String kategori, String lokasi) throws Exception {
        String sql = "UPDATE promo SET nama_promo=?, mulai_promo=?, habis_promo=?, deskripsi=?, kategori=?, lokasi=? WHERE id_promo=?";
        java.sql.Date mulaipromo = new java.sql.Date(mulai_promo.getTime());
        java.sql.Date habispromo = new java.sql.Date(habis_promo.getTime());
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, nama_promo);
            st.setDate(2, mulaipromo);
            st.setDate(3, habispromo);
            st.setString(4, deskripsi);
            st.setString(5, kategori);
            st.setString(6, lokasi);
            st.setInt(7, id);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delete(int id_promo) {

        String sql = "DELETE FROM promo WHERE id_promo = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, id_promo);

            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
